package com.ie.cloud.payment.utils;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class DateUtils {

    /**
     * GMT字符串转Calendar
     */
    public static Calendar gmtToCalendar(String gmtDate) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US);
            Date date = sdf.parse(gmtDate);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            return calendar;
        } catch (ParseException e) {
            throw new IllegalArgumentException("日期格式错误: " + gmtDate, e);
        }
    }

    /**
     * Calendar转GMT字符串
     */
    public static String calendarToGmt(Calendar calendar) {
        SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH);
        return sdf.format(calendar.getTime());
    }


    public static LocalDateTime strToLocalDateTime(String date) {
        DateTime parse = DateUtil.parse(date, DatePattern.NORM_DATETIME_PATTERN);

        Instant instant = parse.toInstant();


        return LocalDateTime.ofInstant(instant, ZoneId.systemDefault());

    }

}
